﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WpfBinding_Source
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //List<Student> studentList = new List<Student>()
            //{
            //    new Student{Id=0, Name="Tim", Age=29},
            //    new Student{Id=1, Name="Tom", Age=28},
            //    new Student{Id=2, Name="Kyle", Age=27},
            //    new Student{Id=3, Name="Tony", Age=26},
            //    new Student{Id=4, Name="Vina", Age=25},
            //    new Student{Id=5, Name="Mike", Age=24},
            //};

            // 为listBox设置Binding
            //this.listBoxStudents.ItemsSource = studentList;
            //this.listBoxStudents.DisplayMemberPath = "Name";

            // 为TextBox设置Binding
            //Binding binding = new Binding("SelectedItem.Id") { Source=this.listBoxStudents};
            //this.textBoxId.SetBinding(TextBox.TextProperty, binding);


            ObjectDataProvider odp = new ObjectDataProvider();
            odp.ObjectInstance = new Calculator();
            odp.MethodName = "Add";
            odp.MethodParameters.Add("0");
            odp.MethodParameters.Add("0");

            Binding bindingToArg1 = new Binding("MethodParameters[0]")
            {
                Source=odp,
                BindsDirectlyToSource=true,
                UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged
            };

            Binding bindingToArg2 = new Binding("MethodParameters[1]")
            {
                Source = odp,
                BindsDirectlyToSource = true,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
            };

            Binding bindingToResult = new Binding(".") {Source=odp };

            // 关联Binding到UI元素
            this.textBoxArg1.SetBinding(TextBox.TextProperty, bindingToArg1);
            this.textBoxArg2.SetBinding(TextBox.TextProperty, bindingToArg2);
            this.textBoxResult.SetBinding(TextBox.TextProperty, bindingToResult);

            RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
            rs.AncestorLevel = 1;
            rs.AncestorType = typeof(Grid);
            Binding binding = new Binding("Name") { RelativeSource=rs };
            this.textBox1.SetBinding(TextBox.TextProperty, binding);
        }

        private void btn_click(object sender, RoutedEventArgs e)
        {
            // DataContext是依赖属性
            //MessageBox.Show(this.btn.DataContext.ToString());
        }

        private void load_Click(object sender, RoutedEventArgs e)
        {
            //XmlDocument doc = new XmlDocument();
            //doc.Load(@"E:\workspace\vs-workspace\wpf-example\WpfBinding-Source\WpfBinding-Source\StudentData.xml");

            //XmlDataProvider xdp = new XmlDataProvider();
            //xdp.Document = doc;

            //// 使用XPath选择需要暴露的数据
            //// 现在是需要暴露一组Student
            //xdp.XPath = @"/StudentList/Student";
            //this.listViewStudents.DataContext = xdp;
            //this.listViewStudents.SetBinding(ListView.ItemsSourceProperty, new Binding());
        }
    }
}
